//
//  JXHTabBarController.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/10/13.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit

class JXHTabBarController: UITabBarController {

    //storyboard也可以设置选择图片
    lazy var imageNames = ["tabbar_home","tabbar_message_center","","tabbar_discover","tabbar_profile"]
    
    lazy var composeButton : UIButton = UIButton.init(imageName: "tabbar_compose_icon_add", bgIamgeName: "tabbar_compose_button")
        
//        .button(imageName: "tabbar_compose_icon_add", bgIamgeName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupComposeBtn()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setupItems()
    }
}

// MARK: - 设置UI
extension JXHTabBarController {

    /// 设置发布按钮
    func setupComposeBtn() {
        tabBar.addSubview(composeButton)
//        composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: .normal)
//        composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: .selected)
//        composeButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: .normal)
//        composeButton.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: .selected)
//        composeButton.sizeToFit()
        composeButton.center = CGPoint.init(x: tabBar.center.x, y: tabBar.frame.size.height * 0.5)
        
        
        //swift中Selector的写法
        //1.Selector("composeButtonClick")
        //2."composeButtonClic"
        //3.swift3 #selector(self.composeButtonClick)
        composeButton.addTarget(self, action: #selector(self.composeButtonClick), for: .touchUpInside)
    }
}


// MARK: - 事件监听
extension JXHTabBarController {

    func composeButtonClick(){
    
        let composeVC = JXHComposeViewController()
        
        let composeNav = UINavigationController.init(rootViewController: composeVC)
        
        present(composeNav, animated: true, completion: nil)
    }
}


extension JXHTabBarController {

    func setupItems() {
        //1.遍历所有的item
        for i in 0..<tabBar.items!.count{
            
            let item = tabBar.items![i]
            
            if i==2 {
                item.isEnabled = false
                continue
            }
            item.selectedImage = UIImage.init(named: imageNames[i] + "_highlighted")
        }
    }
}

